﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data;
using System.Collections;

namespace DAO
{
    public class PhieuNhapDAO
    {
        public static int ThemPhieuNhap(PhieuNhapDTO dv)
        {
            string sql;
            sql = "Insert into PhieuNhap(MaPhieuNhap, NgayNhap) values ('" + dv.MaPhieuNhap + "','" + dv.NgayNhap + "')";
            int kq = DataAccess.ExcuNonQuery(sql);
            return kq;
        }

        public static PhieuNhapDTO LayThongTinPhieuNhap(String maPN)
        {
            PhieuNhapDTO pn = new PhieuNhapDTO();
            string sql = "Select * From PhieuNhap Where MaPhieuNhap = '" + maPN + "'";
            DataTable dt = DataAccess.ExcuQuery(sql);

            if (dt != null && dt.Rows.Count > 0)
            {
                pn.MaPhieuNhap = Convert.ToString(dt.Rows[0]["MaPhieuNhap"]);
                pn.NgayNhap = Convert.ToDateTime(dt.Rows[0]["NgayNhap"]);
                pn.TrangThai = Convert.ToInt32(dt.Rows[0]["TrangThai"]);
            }
            return pn;
        }
        public static int XoaLuon(String ma)
        {
            string sql;
            sql = "DELETE FROM PhieuNhap where MaPhieuNhap = '" + ma + "'";
            int kq = DataAccess.ExcuNonQuery(sql);
            return kq;
        }
        public static int Xoa(String ma)
        {
            string sql;
            sql = "Update PhieuNhap  Set TrangThai=0 where MaPhieuNhap = '" + ma + "'";
            int kq = DataAccess.ExcuNonQuery(sql);
            return kq;
        }
        public static ArrayList LayDanhSach()
        {
            ArrayList arr = new ArrayList();
            string sql;
            sql = "Select * From PhieuNhap where TrangThai=1";
            DataTable dt = DataAccess.ExcuQuery(sql);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                PhieuNhapDTO dv = new PhieuNhapDTO();
                dv.MaPhieuNhap = Convert.ToString(dt.Rows[i]["MaPhieuNhap"]);
                dv.NgayNhap = Convert.ToDateTime(dt.Rows[i]["NgayNhap"]);
                dv.TrangThai = Convert.ToInt32(dt.Rows[i]["TrangThai"]);
                arr.Add(dv);
            }
            return arr;
        }
        public static int CapNhat(PhieuNhapDTO pn)
        {
            string sql;
            sql = "Update PhieuNhap  Set NgayNhap='" + pn.NgayNhap + "', TrangThai= " + pn.TrangThai + " where MaPhieuNhap = '" + pn.MaPhieuNhap + "'";
            int kq = DataAccess.ExcuNonQuery(sql);
            return kq;
        }

    }
}
